﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable
{
	/// <summary>
	/// 工作流模板表单自定义数据库表集合
	/// </summary>
	public class WorkflowTemplateFormCustomDatabaseTableData : IWorkflowTemplateFormCustomDatabaseTableData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplateformcustomdatabasetable";
		private const string QUERY = @"SELECT WorkflowTemplateFormCustomDatabaseTableId,DatabaseComment,DatabaseName,TableComment,TableName,CreateTime,UpdateTime,FormConfigTitle,FormConfigCode FROM oa_workflowtemplateformcustomdatabasetable";
		public WorkflowTemplateFormCustomDatabaseTableData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板表单自定义数据库表集合是否存在
		public async Task<bool> Exists(long workflowTemplateFormCustomDatabaseTableId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateFormCustomDatabaseTableId=?WorkflowTemplateFormCustomDatabaseTableId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFormCustomDatabaseTableId", workflowTemplateFormCustomDatabaseTableId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板表单自定义数据库表集合
		public async Task<WorkflowTemplateFormCustomDatabaseTableEntity> Add(WorkflowTemplateFormCustomDatabaseTableEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplateformcustomdatabasetable", new string[] { "FormConfigTitle", "FormConfigCode", "DatabaseComment", "DatabaseName", "TableComment", "TableName" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateFormCustomDatabaseTableId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?DatabaseComment", entity.DatabaseComment?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DatabaseName", entity.DatabaseName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TableComment", entity.TableComment?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TableName", entity.TableName?.Trim(), DbType.AnsiString, size: 100);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateFormCustomDatabaseTableEntity>(sql, parameters, databaseContext.Transaction);

		}
		public async Task<bool> Add(List<WorkflowTemplateFormCustomDatabaseTableEntity> entities)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplateformcustomdatabasetable", new string[] { "FormConfigTitle", "FormConfigCode", "DatabaseComment", "DatabaseName", "TableComment", "TableName" });
			return await this.databaseContext.Master.ExecuteAsync(sql, entities, databaseContext.Transaction) == entities.Count;

		}
		#endregion

		#region 修改工作流模板表单自定义数据库表集合
		public async Task<WorkflowTemplateFormCustomDatabaseTableEntity> Update(WorkflowTemplateFormCustomDatabaseTableEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplateformcustomdatabasetable", new string[] { "FormConfigTitle", "FormConfigCode", "DatabaseComment", "DatabaseName", "TableComment", "TableName" }, new string[] { "WorkflowTemplateFormCustomDatabaseTableId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateFormCustomDatabaseTableId=?WorkflowTemplateFormCustomDatabaseTableId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFormCustomDatabaseTableId", entity.WorkflowTemplateFormCustomDatabaseTableId, DbType.Int64, size: 20);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?DatabaseComment", entity.DatabaseComment?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DatabaseName", entity.DatabaseName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TableComment", entity.TableComment?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TableName", entity.TableName?.Trim(), DbType.AnsiString, size: 100);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateFormCustomDatabaseTableEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板表单自定义数据库表集合
		public async Task<bool> Delete(long workflowTemplateFormCustomDatabaseTableId)
		{
			string sql = @"DELETE FROM oa_workflowtemplateformcustomdatabasetable WHERE WorkflowTemplateFormCustomDatabaseTableId=?WorkflowTemplateFormCustomDatabaseTableId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFormCustomDatabaseTableId", workflowTemplateFormCustomDatabaseTableId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		public async Task<bool> Delete(string formConfigCode)
		{
			string sql = @"DELETE FROM oa_workflowtemplateformcustomdatabasetable WHERE FormConfigCode=?FormConfigCode";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigCode", formConfigCode, DbType.AnsiString);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合
		public async Task<WorkflowTemplateFormCustomDatabaseTableEntity> GetEntity(long workflowTemplateFormCustomDatabaseTableId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateFormCustomDatabaseTableId=?WorkflowTemplateFormCustomDatabaseTableId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFormCustomDatabaseTableId", workflowTemplateFormCustomDatabaseTableId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateFormCustomDatabaseTableEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合列表
		public async Task<IEnumerable<WorkflowTemplateFormCustomDatabaseTableEntity>> GetList(string formConfigCode)
		{
			string sql = string.Format("{0} WHERE FormConfigCode=?FormConfigCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigCode", formConfigCode.Trim(), DbType.AnsiString, size: 100);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateFormCustomDatabaseTableEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合分页
		public async Task<(IEnumerable<WorkflowTemplateFormCustomDatabaseTableEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplateformcustomdatabasetable {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateFormCustomDatabaseTableEntity, int>(sql, parameters);
		}
		#endregion
	}
}



